package com;

import java.io.IOException;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ConcurrentModificationException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Transaction;

@SuppressWarnings("serial")
public class CreateAuctionServlet extends HttpServlet 
{//client-server style (get input from client, then redirect output)
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException 
	{
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		
		String msg = "";
		String itemName = req.getParameter("itemName");
		String description = req.getParameter("description");
		String basePrice = req.getParameter("basePrice");
		String startDateAndTime = req.getParameter("start");
		String endDateAndTime = req.getParameter("end"); 
		String username = req.getParameter("username");
		String category = req.getParameter("category");
		
		Boolean isDouble=true;
		
		
		if(itemName.equals("") ||description.equals("") ||basePrice.equals("") || 
				startDateAndTime.equals("") ||endDateAndTime.equals("")){
			
			msg = "ERROR. Please enter a value for each field.";
			req.setAttribute("msg", msg);

			
			System.out.println(msg);

			req.getRequestDispatcher("error.jsp").forward(req, resp);
		}else{

			String current="";
			java.util.Date d1,d2,d3;
			
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
			
			java.util.Date currentDate = new java.util.Date();	
			current = df.format(currentDate);

			try {
				d1 = df.parse(startDateAndTime);
				d2 = df.parse(endDateAndTime);
				d3 = df.parse(current);		
				
				
				if(d1.after(d2)){
					//invalid start and end times, start is larger than end value
					msg = "ERROR. Invalid start and end times.";
					req.setAttribute("msg", msg);
					
					System.out.println(msg);
	
					req.getRequestDispatcher("error.jsp").forward(req, resp);
				}else if (d2.before(d3)){
					//invalid end time, end time is too early
					msg = "ERROR. Invalid End time. Auctions must end in the future.";
					req.setAttribute("msg", msg);
					
					System.out.println(msg);
	
					req.getRequestDispatcher("error.jsp").forward(req, resp);
					
					
				}else{
				
					//correct start and end times
					
					try
					{
					  Double.parseDouble(basePrice);
					}
					catch(NumberFormatException e)
					{
						isDouble=false;
					}
					
					if(!isDouble){
						//price is not inputted correctly
						msg = "ERROR. Please enter a valid price.";
						req.setAttribute("msg", msg);
						
						System.out.println(msg);
		
						req.getRequestDispatcher("error.jsp").forward(req, resp);
						
						
					}else{
						
						double price = Math.round(Double.parseDouble(basePrice)*100)/100.0;
						System.out.print(price);
						for (int j = 0; j < 10; j++) {
							Transaction tx = ds.beginTransaction();
							try {
								Entity e = new Entity("Auction");
								e.setProperty("NAME", itemName);
								e.setProperty("DESCRIPTION", description);
								e.setProperty("PRICE", price);
								e.setProperty("START", startDateAndTime);
								e.setProperty("END", endDateAndTime);
								e.setProperty("SELLER",username);
								e.setProperty("CATEGORY",category);
								//Blackboard style
								ds.put(tx, e);
								tx.commit();
								break;
								
								
							} catch (ConcurrentModificationException ed) {
								// continue
							} finally {
								if (tx.isActive())
									tx.rollback();
							}
						}
						req.getRequestDispatcher("sellerIndex.jsp").forward(req, resp);
					}
			}
				
			} catch (ParseException e1) {

				//date is not inputted correctly
				msg = "ERROR. Please enter valid dates. Use the calendar tool.";
				req.setAttribute("msg", msg);
				
				System.out.println(msg);

				req.getRequestDispatcher("error.jsp").forward(req, resp);
			}
				
						
		}
		
	}
}
